package com.security.demo.service;

import com.security.demo.dao.BtUserPrivilegeDao;
import com.security.demo.entity.BtModule;
import com.security.demo.entity.BtUserPrivilege;
import org.apache.ibatis.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: zhsong
 * @time: 2021/9/6 14:57
 */
@Service
public class BtUserPrivilegeService {

    @Autowired
    BtUserPrivilegeDao btUserPrivilegeDao;

    public int insertBtUserModule(String userId,String name,String operationName) throws Exception {
        BtUserPrivilege btUserPrivilege = new BtUserPrivilege();
        btUserPrivilege.setUserId(userId);
        btUserPrivilege.setName(name);
        btUserPrivilege.setOperationName(operationName);
        int res = btUserPrivilegeDao.insertBtUserModule(btUserPrivilege);
        if(res < 1){
            throw new Exception("插入失败");
        }
        return res;
    }
    //删除指定用户对指定模块的指定操作

    public int deleteBtUserModule(String userId,String name,String operationName) throws Exception {
        BtUserPrivilege btUserPrivilege = new BtUserPrivilege();
        btUserPrivilege.setUserId(userId);
        btUserPrivilege.setName(name);
        btUserPrivilege.setOperationName(operationName);
        int res = btUserPrivilegeDao.deleteBtUserModule(btUserPrivilege);
        if(res < 1){
            throw new Exception("插入失败");
        }
        return res;
    }

    //根据用户id查询其某个模块拥有的操作

    public List<String> searchBtUserModule(String userId,String name){
        BtUserPrivilege btUserPrivilege = new BtUserPrivilege();
        btUserPrivilege.setUserId(userId);
        btUserPrivilege.setName(name);
        List<BtUserPrivilege> btUserPrivileges = btUserPrivilegeDao.searchBtUserModule(btUserPrivilege);

        if(btUserPrivilege == null){
            return null;
        }
        List<String> operations = new ArrayList<>();
        for (BtUserPrivilege userPrivilege : btUserPrivileges) {
            operations.add(userPrivilege.getOperationName());
        }

        return operations;
    }

}
